package id.pratama.pantaijogja;

import id.pratama.pantaijogja.util.Utils;

import java.io.File;

import com.google.ads.AdView;
import com.google.android.gms.maps.model.LatLng;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.content.Intent;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class LokasiActivity extends ActionBarActivity implements
		OnClickListener
{

	private ImageView					img;
	private Button						btnLokasi;
	private TextView					txtDeskripsi;
	private AdView						ads;

	private LatLng						locTujuan;

	private ImageLoader					imageLoader;
	private ImageLoaderConfiguration	config;
	private DisplayImageOptions			options;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lokasi);
		img = (ImageView) findViewById(R.id.gambar);
		btnLokasi = (Button) findViewById(R.id.btnMap);
		txtDeskripsi = (TextView) findViewById(R.id.deskripsi);

		btnLokasi.setOnClickListener(this);

		getSupportActionBar().setIcon(android.R.color.transparent);

		// imagecache
		File cacheDir = StorageUtils.getCacheDirectory(this);

		imageLoader = ImageLoader.getInstance();

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_launcher).cacheOnDisc(true)
				.delayBeforeLoading(100).cacheInMemory(true).build();

		config = new ImageLoaderConfiguration.Builder(this)
				.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
				.discCache(new UnlimitedDiscCache(cacheDir))
				.discCacheSize(50 * 1024 * 1024).discCacheFileCount(100)
				.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
				.writeDebugLogs().build();

		imageLoader.init(config);

		Bundle b = getIntent().getExtras();
		if (b != null)
		{
			locTujuan = new LatLng(b.getDouble(MainActivity.TAG_LAT),
					b.getDouble(MainActivity.TAG_LNG));
			getSupportActionBar()
					.setTitle(b.getString(MainActivity.TAG_LOKASI));
			txtDeskripsi.setText(b.getString(MainActivity.TAG_DESC));

			Utils.TRACE("aaa", b.getString(MainActivity.TAG_GAMBAR));

			imageLoader.displayImage(b.getString(MainActivity.TAG_GAMBAR), img,
					options);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public void onBackPressed()
	{
		super.onBackPressed();
		if (ads != null)
			ads.destroy();
		finish();
	}

	@Override
	protected void onDestroy()
	{
		// TODO Auto-generated method stub
		super.onDestroy();
		if (ads != null)
			ads.destroy();
	}

	@Override
	public void onClick(View arg0)
	{
		if (arg0 == btnLokasi)
		{
			Intent i = new Intent(android.content.Intent.ACTION_VIEW,
					Uri.parse("http://maps.google.com/maps?daddr="
							+ locTujuan.latitude + "," + locTujuan.longitude));
			startActivity(i);

		}
	}

}
